package com.viewsky.base.back.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.viewsky.base.back.bean.LoginUser;
import com.viewsky.base.back.config.ServerPropertyConfig;
import com.viewsky.base.back.util.Constant;
import com.viewsky.base.back.util.JwtUtil;

import io.jsonwebtoken.Claims;

public class BaseController
{	
	@Value("${config.authentication.method}")
	protected String strAuthenticationMethod;
	
	@Autowired
	private ServerPropertyConfig serverPropertyConfig;
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	protected LoginUser getUser()
	{
		LoginUser loginUser = null;
		
		try
		{
			RequestAttributes ra = RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();		
			
			if(Constant.AuthenticationMethod.SESSION.equals(strAuthenticationMethod))
			{					
				loginUser = (LoginUser)request.getSession().getAttribute("loginUser");			
			}
			else if(Constant.AuthenticationMethod.TOKEN.equals(strAuthenticationMethod))
			{
				String strToken = request.getHeader("Authorization");
				strToken = strToken.split("^" + Constant.DEF_TOKEN_HEADER)[1];
				
				Claims mapClaim = JwtUtil.decodeToken(strToken);
				loginUser = new LoginUser(
					(String)mapClaim.get("userId"),
					(String)mapClaim.get("userName"), 
					(String)mapClaim.get("orgId"), 						
					(String)mapClaim.get("roleId")
				);
			}
			else 
			{
				throw new Exception("配置的config.authentication.method不合法");
			}
			
			String strClientIp = getClientIp();
			loginUser.setClientIp(strClientIp);
		}
		catch(Exception e)
		{
			logger.error("获取当前用户出错：" + e.toString());
			loginUser = new LoginUser();
		}
		
		return loginUser;
	}
	
	protected String getClientIp()
	{
		String strIp = "unknown";
		
		try
		{
			RequestAttributes ra = RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();		
			
			strIp = request.getHeader("x-forwarded-for");
			if(strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) 
			{
				strIp = request.getHeader("Proxy-Client-IP");
			}
			if(strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) 
			{
				strIp = request.getHeader("WL-Proxy-client-IP");
			}
			if(strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) 
			{
				strIp = request.getRemoteAddr();
			}
		}
		catch(Exception e)
		{
			logger.error("获取客户端IP出错：" + e.toString());
			strIp = "unknown";
		}
		
		return strIp;
	}
	
	protected String getServer()
	{
		return serverPropertyConfig.getServer();
	}
}
